package org.phantom.demo.search.queuetest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

@Component
public class ThreadPool {

	private ExecutorService service = null;

	@PostConstruct
	public void init() {
		service = Executors.newCachedThreadPool();
	}

	public void exec(PriorityBlockingQueue<?> queue) {
		Object o = queue.poll();
		while (o != null) {
			Task task = TaskFactory.getTaskInstance((DataLevel) o);
			service.execute(task);
			o = queue.poll();
		}
	}

	@PreDestroy
	public void destory() {
		service.shutdown();
	}
}
